package com.stone.framework.batch.service;

import org.springframework.stereotype.Component;

import com.stone.framework.batch.entity.CaseAssign;
import com.stone.framework.batch.entity.CaseInfo;
import com.stone.framework.batch.entity.CaseState;
import com.stone.framework.batch.model.CaseAllocBO;
import com.stone.framework.batch.model.CaseAllocResultBO;
import com.stone.framework.core.exception.BusinessException;

@Component
public class AdjustedCaseService extends GenericCaseService {

    @Override
    protected CaseAllocResultBO doAllocate(CaseInfo currCaseInfo, CaseAllocBO caseAllocBo) {
        if (currCaseInfo == null) {
            throw new BusinessException("案件不存在或已出催");
        }
        CaseInfo closedCaseInfo = this.buildClosedCaseInfo(currCaseInfo);
        CaseInfo newCaseInfo = this.buildNewCaseInfo(caseAllocBo, currCaseInfo);
        CaseState updateCaseState = this.buildUpdateCaseState(newCaseInfo,
            newCaseInfo.getCollector() == null ? CaseState.State.UNALLOC : CaseState.State.ALLOC);
        CaseAssign newCaseAssign = this.buildNewCaseAssign(newCaseInfo, caseAllocBo, CaseAssign.Type.ADJUST);
        return new CaseAllocResultBO().closedCaseInfo(closedCaseInfo).newCaseInfo(newCaseInfo)
            .updateCaseState(updateCaseState).newCaseAssign(newCaseAssign).build();
    }

}
